﻿using System.Configuration;
using System.Data.Common;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Linq;

namespace Eff.Data.Linq
{

    public class DataContext<T> : DataContext where T : class
    {
        public Table<T> Table { get; private set; }
        public DataContext()
            : base(ConfigurationManager.ConnectionStrings[ConnStrHelper.DefaultConnectionName].ConnectionString)
        {
            Table = GetTable<T>();
        }

        public DataContext(string connectionName)
            : base(ConfigurationManager.ConnectionStrings[connectionName].ConnectionString)
        {
            Table = GetTable<T>();
        }

        public DataContext(DbConnection connection, DbTransaction trans)
            : base(connection)
        {
            Transaction = trans;
            Table = GetTable<T>();
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="query">
        /// System.Linq.IQueryable&lt;T&gt;对象
        /// <para>Linq中使用的查询对象，数据查询源</para>
        /// </param>
        /// <param name="pageInfo">分页信息结构体</param>
        /// <returns>
        /// System.Linq.IQueryable&lt;T&gt;对象
        /// <para>Linq中使用的查询对象，该对象已经进行了分页查询操作</para>
        /// </returns>
        protected IQueryable<T> Page(IQueryable<T> query, PageInfo pageInfo)
        {
            return Page<T>(query, pageInfo);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="TK">Linq中使用的Table类</typeparam>
        /// <param name="query">
        /// System.Linq.IQueryable&lt;T&gt;对象
        /// <para>Linq中使用的查询对象，数据查询源</para>
        /// </param>
        /// <param name="pageInfo">分页信息结构体</param>
        /// <returns>
        /// System.Linq.IQueryable&lt;T&gt;对象
        /// <para>Linq中使用的查询对象，该对象已经进行了分页查询操作</para>
        /// </returns>
        protected IQueryable<TK> Page<TK>(IQueryable<TK> query, PageInfo pageInfo)
        {
            pageInfo.RecordCount = query.Count();

            var pageSize = pageInfo.PageSize; 
            var skipCount = (pageInfo.CurrentPage - 1) * pageSize;

            query = query.Skip<TK>(skipCount).Take<TK>(pageSize);
            return query;
        }
    }

    /// <summary>
    /// 本系统数据库链接的帮助类
    /// </summary>
    public class ConnStrHelper
    {
        /// <summary>
        /// 默认数据库链接配置名
        /// </summary>
        public const string DefaultConnectionName = "default";

        /// <summary>
        /// 获取数据库链接
        /// </summary>
        /// <param name="connectionName">配置链接名</param>
        /// <returns>数据库链接字符串</returns>
        public static string GetConnStr(string connectionName = DefaultConnectionName)
        {
            return ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
        }

        /// <summary>
        /// 创建默认的数据库链接
        /// </summary>
        /// <returns></returns>
        public static DbConnection CreateConnection()
        {
            var sqlCon = new SqlConnection(GetConnStr());

            return sqlCon;
        }
    }
}